﻿using MengXunAppTool.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using 软件发布平台.ViewModel;

namespace 软件发布平台.View
{
    /// <summary>
    /// TreeView.xaml 的交互逻辑
    /// </summary>
    public partial class TreeView : UserControl
    {
        public TreeView(ViewModel.ReleaseViewModel viewModel )
        {
            InitializeComponent();
            DataContext = viewModel;
        }
        private void CheckBox_Checked(object sender, RoutedEventArgs e)
        {
            try
            {
                TreeViewItem target = GetNearestContainer(e.OriginalSource as UIElement);
                if (target != null)
                {
                    CheckBoxTreeViewModel CheckModel = target.Header as CheckBoxTreeViewModel;
                    if (CheckModel != null)
                    {
                        //if (!configFiles.Contains(CheckModel.Path))
                        //{
                        //    configFiles.Add(CheckModel.Path);
                        //}
                    }
                    else
                    {

                    }
                }
            }
            catch (Exception ee)
            {
                HandyControl.Controls.Growl.Error(ee.Message.ToString());
                LogHelper.WriteLog(ee.ToString());
            }
        }

        private void CheckBox_Unchecked(object sender, RoutedEventArgs e)
        {
            try
            {
                TreeViewItem target = GetNearestContainer(e.OriginalSource as UIElement);
                if (target != null)
                {
                    CheckBoxTreeViewModel CheckModel = target.Header as CheckBoxTreeViewModel;
                    if (CheckModel != null)
                    {
                        //if (configFiles.Contains(CheckModel.Path))
                        //{
                        //    configFiles.Remove(CheckModel.Path);
                        //}
                    }
                    else
                    {

                    }
                }
            }
            catch (Exception ee)
            {
                HandyControl.Controls.Growl.Error(ee.Message.ToString());
                LogHelper.WriteLog(ee.ToString());
            }
        }
        public TreeViewItem GetNearestContainer(UIElement element)
        {
            // 沿着元素树向上走到最近的树视图项。
            TreeViewItem container = element as TreeViewItem;
            while ((container == null) && (element != null))
            {
                element = VisualTreeHelper.GetParent(element) as UIElement;
                container = element as TreeViewItem;
            }
            return container;
        }
        CheckBoxTreeViewModel LastSelectModel;
        private void Tree_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            try
            {
                TreeViewItem target = GetNearestContainer(e.OriginalSource as UIElement);
                if (target != null)
                {
                    target.IsSelected = true;
                    CheckBoxTreeViewModel SelectModel = target.Header as CheckBoxTreeViewModel;
                    if (SelectModel != null)
                    {
                        if (SelectModel.Path.Substring(SelectModel.Path.Length - 4).Equals(".exe"))
                        {
                            if (LastSelectModel != null)
                            {
                                LastSelectModel.ForeColor = new SolidColorBrush(Color.FromRgb(0, 0, 0));
                            }
                            SelectModel.ForeColor = new SolidColorBrush(Color.FromRgb(50, 200, 50));
                            //exePath = SelectModel.Path;
                            LastSelectModel = SelectModel;
                        }
                    }
                }
            }
            catch (Exception ee)
            {
                HandyControl.Controls.Growl.Error(ee.Message.ToString());
                LogHelper.WriteLog(ee.ToString());
            }
        }
    }
}
